// index.js
// 获取应用实例
import http from '../../utils/request'
const app = getApp()
Page({
  data: {
    ydIshow:true,
    show:false,
    selectActive:'高中',
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 500,
    banner: [],
    ishowreport: true,
    tbbklist: [{
      hoverIshow: true,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '同步试题'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '课件'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '教案'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '导学案'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '素材'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '试卷'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '习题'
    }
    ],
    tbbklist1: [{
      hoverIshow: true,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '阶段检测'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '周测试卷'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '月考检测'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '期中试卷'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '期末检测'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '竞赛测试'
    },
    {
      hoverIshow: false,
      name: '云南省2023届高三第二次复习统一检测语',
      area: '江苏省',
      download: 124,
      looknum: 256,
      time: '08-17',
      type: '复习试卷'
    }
    ],
    hightmiddlelist: [],
    ebooklist: []
  },
  onShow() {
    this.getMhkao() //优质资源
  },
  onShareAppMessage: function(options) {
    // 返回自定义分享信息
    return {
      title: '百师云资源网',
      path: '/pages/index/index', // 分享页面路径
    //   imageUrl: '/path/to/image.jpg', // 分享图标
      success: function(res) {
        // 分享成功
      },
      fail: function(res) {
        // 分享失败
      }
    };
  },
  onShareTimeline: function(options) {
    // 返回自定义分享信息
    return {
      title: '百师云资源网', // 分享标题
    //   imageUrl: '/path/to/image.jpg', // 分享图标
    //   query: 'param1=value1&param2=value2', // 分享时携带的参数
      success: function(res) {
        // 分享成功
      },
      fail: function(res) {
        // 分享失败
      }
    };
  },
  onLoad() {
    this.getBannerBox()//获取banner
    this.getResouceClassify() //获取分类标签
    this.getSpecialClassify() //获取专题分类列表
    this.recommedSpecial()//推荐专题
    this.hightmiddleclick('高中') //中高考资源
    this.ebooklistclick()//教辅出版
  },
  ydclose(){
    this.setData({ydIshow:false})
  },
  // 获取教辅出版
  async ebooklistclick() {
    // getEbookResourcebundle
    let options = {
      url: '/getEbookResourcebundle',
      method: 'POST',
      data: { labelitemcaseId: 0, subjectId: 0, level2Id: 0, page: 1, pagesize: 5 },
      header: { "content-type": "application/json;charset=UTF-8" }
    }
    let res = await http(options)
    if (res.data.code == 0) {
      this.setData({ ebooklist: res.data.obj.resultRBs })
    } else {
      wx.showToast({
        title: res.data.msg,
      })
    }
  },
  // 获取中高考资源
  async hightmiddleclick(name) {
    let options = {
      url: '/getIsShowItemCases',
      method: 'POST',
      data: { n1: name },
      header: { "content-type": "application/json;charset=UTF-8" }
    }
    let res = await http(options)
    if (res.data.code == 0) {
      res.data.obj.map(item => {
        let extea = item.itemname.substring(item.itemname.lastIndexOf('.') + 1).toLowerCase().replace(/\s+/g, "")
        return Object.assign(item, { suffix: extea })
      })
      this.setData({ hightmiddlelist: res.data.obj })
    } else {
      wx.showToast({
        title: res.data.msgs,
      })
    }
  },
  //年度报告
  // reportlink(){
  //   wx.navigateTo({
  //     url: '/pages/annualreportindex/annualreportindex',
  //   })
  // },
  reportClose(){
    this.setData({ishowreport:false})
  },
  // 获取初高中
  async getMhkao1() {
    let that = this
    let options = {
      url: '/getNavs5',
      method: 'POST',
      data: { id: 0 },
      header: { "content-type": "application/json;charset=UTF-8" }
    }
    let nes = []
    let res = await http(options)
    nes = res.data.filter(item => item.name != '不限')
    that.setData({ mhName: nes })
  },
  close() {
    this.setData({ show: false })
  },
  selectButton(e) {
    let index = e.currentTarget.dataset['index'];
    let item = e.currentTarget.dataset['item'].name;
    this.setData({ selectActive: item, show: false })
    wx.setStorageSync('mhName', e.currentTarget.dataset['item'])
    this.hightmiddleclick(item)
    // jsonToString
  },
  //教辅更多
  ebookMore() {
    wx.navigateTo({
      url: '/pages/ebookIndex/ebookIndex',
    })
  },
  // 初高中切换
  gaozhongTag() {
    this.getMhkao1()//中高考
    this.setData({ show: true })
  },
  //获取banner图
  async getBannerBox() {
    let that = this
    let options = {
      url: '/getRecommenditemcase',
      method: 'POST',
      data: { id: 0 }
    }
    let res = await http(options)
    that.setData({ banner: res.data.obj })
  },
  // banner跳转
  bannerlist(e) {
    let link = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.id
    let icon = e.currentTarget.dataset.icon
    let name = e.currentTarget.dataset.name
    // console.info(id)
    if (link) {
      if (link.includes('https://') || link.includes('http://')) {
        wx.navigateTo({
          url: '/pages/webview/webview?url=' + encodeURIComponent(link),
        })
      } else {
        if (id) {
            wx.navigateTo({
              url:`/pages${link}${link}?adv=${icon}&type=${id}&typename=${name}`
            })
        }else if(link.includes('/pages')){
          wx.navigateTo({
            url:link
          })
        }else{
          if (link.includes('专题')) {
            wx.navigateTo({
              url: link
            })
          } else {
            if (link.includes('?')) {
              let linkId = link.split('?')
              wx.navigateTo({
                url: `/pages${linkId[0]}${linkId[0]}?${linkId[1]}`
              })
            } else {
              wx.navigateTo({
                url: `/pages${link}${link}`
              })
            }
          }
      }
    }
    }
  },
  async getResouceClassify() {
    let that = this
    let options = {
      url: '/getSource2',
      method: 'POST',
      data: { id: 0 }
    }
    let res = await http(options)
    that.setData({ resouceClassify: res.data.obj })
  },
  // 资源分类标签
  navlinkurl(e) {
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.sourcename
    let type = e.currentTarget.dataset.parameter
    if (name == '专题') {
      wx.navigateTo({
        url: "/pages/speciallist/speciallist"
      })
    } else {
      let params = JSON.stringify({ xueduan: name, xueduanId: id, xueduanleixing: type })
      wx.navigateTo({
        url: "/pages/resoucelist/resoucelist?classifyQuery=" + params
      })
    }
  },
  resouceLink(e) {
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.sourcename
    let type = e.currentTarget.dataset.parameter
    let url = e.currentTarget.dataset.url
    if (name == '全部') {
      wx.navigateTo({
        url: "/pages/resoucelist/resoucelist"
      })
    } else if (name == '中高考') {
      wx.navigateTo({
        url: "/pages/resoucelist/resoucelist?isShow=1"
      })
    } else if (name == '专题') {
      wx.navigateTo({
        url: "/pages/speciallist/speciallist"
      })
    } else if (name == '电子书') {
      wx.navigateTo({
        url: "/pages/ebookIndex/ebookIndex"
      })
    } else {
      let params = JSON.stringify({ xueduan: name, xueduanId: id, xueduanleixing: type })
      wx.navigateTo({
        url: "/pages/resoucelist/resoucelist?classifyQuery=" + params
      })
    }
    // if(url){
    //   // location.href = url
    // }else{
    //   if(name=='全部'){
    //     wx.navigateTo({
    //       url:"/pages/resoucelist/resoucelist"
    //     })
    //   }else{
    //     let params = JSON.stringify({xueduan:name,xueduanId:id,xueduanleixing:type})
    //     wx.navigateTo({
    //       url:"/pages/resoucelist/resoucelist?classifyQuery="+params
    //     })
    //   }
    // }
  },
  //获取专题分类列表
  async getSpecialClassify() {
    let that = this
    let options = {
      url: '/getSpecial',
      method: 'POST',
      data: { id: 0 }
    }
    let res = await http(options)
    that.setData({ specialClassify: res.data.obj })
  },
  // 分类标签
  speciallistRouter(e) {
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.languagename
    wx.navigateTo({
      url: `/pages/speciallist/speciallist?id=${id}&name=${name}`
    })
  },
  // 推荐专题
  async recommedSpecial() {
    let that = this
    let options = {
      url: '/getResourcebundles2',
      method: 'POST'
    }
    let res = await http(options)
    // console.info(res)
    res.data.obj.map(item => { item.neRbname = item.rbName.replace(/\<br>/g, "") })
    that.setData({ speciallist: res.data.obj })
  },
  //专题跳转
  linkRouter(e) {
    let title = e.currentTarget.dataset.name
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/specialdetail/specialdetail?title=${title}&id=${id}`
    });
  },
  // 专题资源跳转全部
  allSpecial(e) {
    let title = e.currentTarget.dataset.type
    if (title == '专题') {
      wx.navigateTo({
        url: '/pages/speciallist/speciallist'
      });
    } else {
      wx.navigateTo({
        url: '/pages/resoucelist/resoucelist'
      });
    }
  },
  // 优质资源
  async getMhkao() {
    let that = this
    let options = {
      url: '/getThemetypeItemCases',
      method: 'POST'
    }
    let res = await http(options)
    res.data.obj.map(item => {
      let extea = item.itemname.substring(item.itemname.lastIndexOf('.') + 1).toLowerCase().replace(/\s+/g, "")
      return Object.assign(item, { suffix: extea })
    })
    that.setData({ mhList: res.data.obj })
  },
  //资源详情跳转
  linkDetail(e) {
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/resoucedetail/resoucedetail?id=' + id + '&title=' + name,
    })
  },
  //搜所跳转
  setarchbutton() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  stringToJson(data) {
    return JSON.parse(data);
  },
  /**
  *字符串转json
  */
  jsonToString(data) {
    return JSON.stringify(data);
  }
})
